/**
 * 
 */
package org.gigi.security.spring;

import java.util.Collection;
import java.util.HashSet;

import org.gigi.security.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 用于Spring Security的扩展User.
 *
 * @author Eugene Wang 2011-11-11
 * @see
 */
public class GigiUserDetails extends User implements UserDetails {
	private static final long serialVersionUID = 3454743567981761449L;
	private Collection<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();

	/**
	 * 默认构造函数
	 */
	public GigiUserDetails() {
	};

	/**
	 * 构造函数。
	 * @param user
	 */
	public GigiUserDetails(User user) {
		setUsername(user.getUsername());
		setPassword(user.getPassword());
		setEmail(user.getEmail());
		setEnabled(user.isEnabled());
		setAccountNonExpired(user.isAccountNonExpired());
		setAccountNonLocked(user.isAccountNonLocked());
		setCredentialsNonExpired(user.isCredentialsNonExpired());
	}

	/* (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	/**
	 * 添加权限。
	 * @param authority
	 */
	public void addAuthority(GrantedAuthority authority) {
		authorities.add(authority);
	}

	/**
	 * 添加所有权限
	 * @param authorites
	 */
	public void addAllAuthority(Collection<GrantedAuthority> authorites) {
		this.authorities.addAll(authorites);
	}
}
